package com.ellecity06.cwhiskymall.ui.invite.more

import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.chad.library.adapter.base.QuickAdapterHelper
import com.chad.library.adapter.base.loadState.LoadState
import com.chad.library.adapter.base.loadState.trailing.TrailingLoadStateAdapter
import com.ellecity06.common.adapter.CustomLoadMoreAdapter
import com.ellecity06.common.base.BaseMvvmActivity
import com.ellecity06.common.ktx.finishRefresh
import com.ellecity06.common.ktx.observeEvent
import com.ellecity06.common.ktx.theme
import com.ellecity06.cwhiskymall.R
import com.ellecity06.cwhiskymall.adapter.MemberInviteListAdapter
import com.ellecity06.cwhiskymall.databinding.ActivityInviteListBinding
import com.ellecity06.cwhiskymall.ui.invite.InviteFriendIntent
import com.ellecity06.cwhiskymall.ui.invite.InviteFriendState
import com.ellecity06.cwhiskymall.ui.invite.InviteFriendViewModel

/**
 * @author ellecity06
 * @time 2024/11/5 11:54
 * @des
 */
class InviteListActivity : BaseMvvmActivity<ActivityInviteListBinding, InviteFriendViewModel>(),
    TrailingLoadStateAdapter.OnTrailingListener, SwipeRefreshLayout.OnRefreshListener {
    private val adapter: MemberInviteListAdapter by lazy { MemberInviteListAdapter() }
    private lateinit var quickAdapterHelper: QuickAdapterHelper
    override fun inflateBinding(): ActivityInviteListBinding {
        return ActivityInviteListBinding.inflate(layoutInflater)
    }

    override fun initialize(savedInstanceState: Bundle?) {
        binding.refreshLayout.theme()
        title = "我的邀请"
        initView()
        refresh()

    }

    override fun initEvent() {
        binding.refreshLayout.setOnRefreshListener(this)
    }

    private fun loadMore() {
        lifecycleScope.launchWhenCreated {
            viewModel.intentChannel.send(InviteFriendIntent.LoadMore)
        }
    }

    private fun refresh() {
        lifecycleScope.launchWhenCreated {
            viewModel.intentChannel.send(InviteFriendIntent.Refresh)
        }
    }

    override fun initObserver() {
        viewModel.state.observeEvent(this) {
            when (it) {
                is InviteFriendState.RefreshSuccess -> {
                    binding.refreshLayout.finishRefresh()
                    adapter.submitList(it.data)
                    if (it.hasMore) {
                        quickAdapterHelper.trailingLoadState = LoadState.NotLoading(false)
                    } else {
                        quickAdapterHelper.trailingLoadState = LoadState.None
                    }

                }
                is InviteFriendState.LoadMoreSuccess -> {
                    binding.refreshLayout.finishRefresh()
                    adapter.addAll(it.data)
                    quickAdapterHelper.trailingLoadState = LoadState.NotLoading(!it.hasMore)
                }
                is InviteFriendState.LoadError -> {
                    binding.refreshLayout.finishRefresh()
                }
                else -> {}
            }
        }
    }

    private fun initView() {
        binding.rv.layoutManager = LinearLayoutManager(this)
        adapter.setEmptyViewLayout(this, R.layout.layout_empty_view)
        adapter.isEmptyViewEnable = true
        val customLoadMoreAdapter = CustomLoadMoreAdapter()
        customLoadMoreAdapter.setOnLoadMoreListener(this)
        quickAdapterHelper = QuickAdapterHelper.Builder(adapter)
            .setTrailingLoadStateAdapter(customLoadMoreAdapter)
            .build()
        binding.rv.adapter = quickAdapterHelper.adapter
    }

    override fun onFailRetry() {
        loadMore()
    }

    override fun onLoad() {
        loadMore()
    }

    override fun onRefresh() {
        refresh()
    }
}